package com.qiwenshare.file.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.file.api.IUserService;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.dto.user.RegisterDTO;
import com.qiwenshare.file.util.FileUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Tag(name = "shelfdata", description = "该接口用来管理书架数据")
@RestController
@Slf4j
@RequestMapping("/shelfdata")
public class ShelfDataController {
    public static final String CURRENT_MODULE = "书籍数据管理";
    public String shelfdataPath = "/mnt/sdc/shelfbooks.json";
    public String noticedataPath = "/mnt/sdc/notice.json";

    @Resource
    IUserService userService;

    @Operation(summary = "书籍数据上传", description = "书籍数据上传", tags = {"shelfdata"})
    @PostMapping(value = "/upload")
    @MyLog(operation = "书籍数据上传", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> upload(@RequestBody String shelfdata,@RequestHeader("token") String token) {
        UserBean sessionUserBean = userService.getUserBeanByToken(token);

        FileUtils.saveJson2jsonfile(shelfdata,shelfdataPath);
        String jsondata = FileUtils.readJsonFile(shelfdataPath);
        return RestResult.success().data(jsondata);
    }

    @Operation(summary = "书籍数据获取", description = "书籍数据获取", tags = {"shelfdata"})
    @GetMapping(value = "/get")
    @MyLog(operation = "书籍数据获取", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> get() {
        String jsondata = FileUtils.readJsonFile(shelfdataPath);
        return RestResult.success().data(jsondata);
    }

    @Operation(summary = "书籍广播更新", description = "书籍广播更新", tags = {"notice"})
    @PostMapping(value = "/setnotice")
    @MyLog(operation = "书籍广播更新", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> setnotice(@RequestBody String notice,@RequestHeader("token") String token) {
        //UserBean sessionUserBean = userService.getUserBeanByToken(token);
        FileUtils.saveJson2jsonfile(notice,noticedataPath);
        String jsondata = FileUtils.readJsonFile(noticedataPath);
        return RestResult.success().data(jsondata);
    }

    @Operation(summary = "书籍广播获取", description = "书籍广播获取", tags = {"notice"})
    @GetMapping(value = "/getnotice")
    @MyLog(operation = "书籍广播获取", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> getnotice() {
        String jsondata = FileUtils.readJsonFile(noticedataPath);
        return RestResult.success().data(jsondata);
    }
}
